package cl.orangepeople.whyem.portal.backend.api.impl;

import cl.orangepeople.whyem.portal.backend.api.NegotiationsService;
import cl.orangepeople.whyem.portal.backend.api.utils.BackendObjectsFactory;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDTO;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDataBean;
import cl.orangepeople.whyem.portal.backend.dto.negotiations.NegotiationDataDetails;
import cl.whyem.sir.ws.ConsultaNegociacionResult;
import cl.whyem.sir.ws.Negociacion;
import cl.whyem.sir.ws.NegociacionServices;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

public class NegotiationServiceImpl implements NegotiationsService {

    private static Logger logger;
    private static String[] clientsID;
    private static String[] paymentNumbers;
    private static String[] productsName;
    private NegociacionServices negociacionServices;

    @Override
    public NegotiationDataBean getNegotiationDetail(NegotiationDTO negotiationData) {
        ConsultaNegociacionResult result = negociacionServices.consultaNegociacion(BackendObjectsFactory.newInstanceHeader(),
                                                                BackendObjectsFactory.newInstanceRut(negotiationData.getClientID()),
                                                                Integer.valueOf(negotiationData.getNegotiationID()));
        NegotiationDataBean bean = new NegotiationDataBean();
        NegotiationDataDetails details = new NegotiationDataDetails();
        if (result.getStatus().getCodigo() == 0) {
            Negociacion negociacion = result.getNegociaciones().get(0);
            details.setAmount(negociacion.getMonto()+"");
            details.setClientID(new BigDecimal(negociacion.getRutDeudor().getNumero()));
            details.setClientName(negociacion.getNombreDeudor());
            details.setDescription(negociacion.getTipoDeuda());
            details.setPaymentNumber(new BigDecimal(negociacion.getIdNegociacion()));
            bean.setReturnCode(0);
            bean.setMessage(result.getStatus().getMensaje());
            bean.setNegotiationDetail(details);            
        } else {
            bean.setReturnCode(result.getStatus().getCodigo());
            bean.setMessage(result.getStatus().getMensaje());
            bean.setNegotiationDetail(details);
        } 
        return bean;
    }

    @Override
    public List<NegotiationDataBean> getNegotiationsByClientID(String clientID) {
         List<NegotiationDataBean> negotiations = new ArrayList<>();
        boolean existClientID = ArrayUtils.contains(clientsID, clientID);
        if (existClientID) {
            for (int i = 1; i <= 4; i++) {
                NegotiationDataBean bean = new NegotiationDataBean();
                NegotiationDataDetails beanDetails = new NegotiationDataDetails();
                bean.setReturnCode(0);
                bean.setNegotiationDetail(beanDetails);
                beanDetails.setClientID(new BigDecimal(clientID));
                beanDetails.setClientName("Paolo Mezzano");
                beanDetails.setDescription(productsName[new Random().nextInt(productsName.length)]);
                beanDetails.setPaymentNumber(new BigDecimal(321654980 + i));
                Locale locale = new Locale("es", "CL");
                NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(locale);
                String trxAmmountFormatted = numberFormatter.format((int) ((Math.random() * 5) * 100000));
                trxAmmountFormatted = StringUtils.remove(trxAmmountFormatted, "Ch");
                beanDetails.setAmount(trxAmmountFormatted);
                negotiations.add(bean);
            }
        }
        return negotiations;
    }

    public NegociacionServices getNegociacionServices() {
        return negociacionServices;
    }

    public void setNegociacionServices(NegociacionServices negociacionServices) {
        this.negociacionServices = negociacionServices;
    }
    
    
}